//
//  BaseViewController.swift
//  Eternity
//
//  Created by ZhengWei on 2016/11/27.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController {

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        self.edgesForExtendedLayout = UIRectEdge.all
        self.navigationController?.navigationBar.isTranslucent = false
//        self.automaticallyAdjustsScrollViewInsets = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - event response
    func rightItemAction(sender: UIButton) {
        
    }
    
    func leftItemAction(sender: UIButton) {
        _ = self.navigationController?.popViewController(animated: true)
    }
    
    // MARK: - private method
    func initNavTitleView(controller: UIViewController) {
        let titleView = UIImageView(frame: CGRect(x: 0, y: 0, width: 90, height: 18))
        titleView.image = UIImage(named: "icon_harmay")
        controller.navigationItem.titleView = titleView
    }
    
    func initNavTitle(title: String, controller: UIViewController) {
        controller.navigationItem.title = title
    }
    
    func initNavItem(controller: UIViewController, imageName: String, highlitedImageName: String, selectedImageName: String, isLeft: Bool) {
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: highlitedImageName), for: .highlighted)
        button.setImage(UIImage(named: selectedImageName), for: .selected)
        
        if isLeft {
            button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -40, bottom: 0, right: -15)
            controller.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
            button.addTarget(self, action: #selector(BaseViewController.leftItemAction(sender:)), for: .touchUpInside)
        } else {
            button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: -40)
            controller.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
            button.addTarget(self, action: #selector(BaseViewController.rightItemAction(sender:)), for: .touchUpInside)
        }
    }
    

}
